package com.zh.note.aop;

import com.zh.note.annotation.CheckTooFrequentCommit;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

/**
 * @Author: zhanghuan
 * @date: 2023/9/13 16:06
 * @description: create it
 */
@Aspect
public class CheckTooFrequentCommitAspect {
    public static final Logger LOGGER = LoggerFactory.getLogger(CheckTooFrequentCommitAspect.class);

    @Around("@annotation(com.zh.note.annotation.CheckTooFrequentCommit)")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        LOGGER.info("进入切面");

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        CheckTooFrequentCommit declaredAnnotation = method.getDeclaredAnnotation(CheckTooFrequentCommit.class);
        String message = declaredAnnotation.value();
        //调用目标接口，返回值为接口原返回值（也可以不调用，就相当跳过接口原要执行的方法）
        Object result = joinPoint.proceed();

        return result;//可自己在这里修改接口返回的值
    }
}
